package com.greenantmall.home;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.greenantmall.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

public class AdapterGridStoreStyle extends BaseAdapter {

	private class GridHolder {
		ImageView icon;
		TextView title;
		TextView subtitle;
	}

	private Context context;

	private List<StoreStyleInfo> list;
	private LayoutInflater mInflater;
	private ImageLoadingListener animateFirstListener = new StoreStyleListener();
	private DisplayImageOptions options;

	public AdapterGridStoreStyle(Context c) {
		super();
		this.context = c;
		
		options = new DisplayImageOptions.Builder()
			.showImageOnLoading(R.drawable.ic_stub)
			.showImageForEmptyUri(R.drawable.ic_empty)
			.showImageOnFail(R.drawable.ic_error)
			.cacheInMemory(true)
			.cacheOnDisk(true)
			.considerExifParams(true)
			.displayer(new RoundedBitmapDisplayer(20)).build();
	}

	public void setList(List<StoreStyleInfo> list) {
		this.list = list;
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

	}

	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int index) {

		return list.get(index);
	}

	@Override
	public long getItemId(int index) {
		return index;
	}

	@Override
	public View getView(int index, View convertView, ViewGroup parent) {
		GridHolder holder;
		if (convertView == null) {   
			convertView = mInflater.inflate(R.layout.item_store_style, null);   
			holder = new GridHolder();
			holder.icon = (ImageView)convertView.findViewById(R.id.iv_icon);
			holder.title = (TextView)convertView.findViewById(R.id.tv_title);
			holder.subtitle = (TextView)convertView.findViewById(R.id.tv_subtitle);
			convertView.setTag(holder);   

		}else{
			 holder = (GridHolder) convertView.getTag();   

		}
		if (index<=this.getCount()){
			StoreStyleInfo info = list.get(index);
			if (info != null) {   
				holder.title.setText(info.getTitle());
				holder.subtitle.setText(info.getSubtitle());
				if (info.getUrl()!=null)
				{
					ImageLoader.getInstance().displayImage(info.getUrl(), holder.icon, options, animateFirstListener);
				}
				//holder.appImage.setImageResource(R.drawable.fresh);
			}
		}
		return convertView;
	}

	private static class StoreStyleListener extends SimpleImageLoadingListener {

		static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

		@Override
		public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
			if (loadedImage != null) {
				ImageView imageView = (ImageView) view;
				boolean firstDisplay = !displayedImages.contains(imageUri);
				if (firstDisplay) {
					FadeInBitmapDisplayer.animate(imageView, 500);
					displayedImages.add(imageUri);
				}
			}
		}
	}
}
